<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="用户手机号" prop="phoneNumber">
        <el-input v-model="queryParams.phoneNumber" placeholder="请输入用户手机号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input v-model="queryParams.nickname" placeholder="请输入用户昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- 导出 -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['user:user:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" type="index" />
      <el-table-column label="用户头像地址" align="center" prop="avatarUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatarUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="用户手机号" align="center" prop="phoneNumber" />
      <el-table-column label="用户昵称" align="center" prop="nickname" />
      <el-table-column label="用户性别" align="center" prop="gender">
        <template slot-scope="scope">
          {{ scope.row.gender === 0 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column label="用户生日" align="center" prop="birthday" />
      <el-table-column label="用户状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="#ff4949" inactive-color="#13ce66" :active-value="1"
            :inactive-value="0" @change="handleChangeStatus(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- 将编辑和删除按钮修改为详情按钮 -->
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)"
            v-hasPermi="['user:user:detail']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 用户详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form label-width="100px">
            <el-form-item label="用户手机号">
              {{ form.phoneNumber }}
            </el-form-item>
            <el-form-item label="用户昵称">
              {{ form.nickname }}
            </el-form-item>
            <el-form-item label="用户性别">
              {{ form.gender === 0 ? '男' : '女' }}
            </el-form-item>
            <el-form-item label="用户头像">
              <image-preview :src="form.avatarUrl" :width="50" :height="50" />
            </el-form-item>
            <el-form-item label="用户状态">
              {{ form.status === 0 ? '正常' : '禁用' }}
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24">
          <el-table :data="form.addressList" style="width: 100%">
            <el-table-column prop="provinceName" label="省份" />
            <el-table-column prop="cityName" label="城市" />
            <el-table-column prop="districtName" label="区县" />
            <el-table-column prop="detailAddress" label="详细地址" />
            <el-table-column prop="buildingOrHouseNumber" label="门牌号" />
            <el-table-column prop="contactName" label="联系人" />
            <el-table-column prop="phoneNumber" label="联系电话" />
            <el-table-column prop="isDefault" label="是否默认">
              <template slot-scope="scope">
                {{ scope.row.isDefault ? '是' : '否' }}
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, updateUser } from "@/api/user/user";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true, // 初始化 showSearch
      // 总条数
      total: 0,
      // 用户信息表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phoneNumber: null,
        nickname: null,
      }, // 初始化 queryParams
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phoneNumber: [
          { required: true, message: "用户手机号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },

    /** 下载模板操作 */
    importTemplate() {
      this.download('user/user/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
    /** 查询用户信息列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        phoneNumber: null,
        nickname: null,
        gender: null,
        avatarUrl: null,
        status: null,
        createTime: null,
        updateTime: null,
        isDeleted: null,
        addressList: [] // 初始化 addressList
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id;
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "用户详情";
      });
    },
    /** 状态改变时的操作 */
    handleChangeStatus(row) {
      const params = {
        id: row.id,
        status: row.status
      };
      updateUser(params).then(() => {
        this.$modal.msgSuccess('状态更新成功');
      }).catch(() => {
        // 如果更新失败，恢复原状态
        row.status = row.status === 0 ? 1 : 0;
      });
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.el-form-item {
  margin-bottom: 15px;
}

.el-table {
  margin-top: 20px;
}
</style>